﻿using NModbus;
using NModbus.Serial;
using System.Diagnostics;
using System.IO.Ports;
using System;

namespace ModuleBase
{
    public class ServoModbusService : IServoModbusService
    {
        public bool Connected { get; set; }
        System.IO.Ports.SerialPort serialPort = new SerialPort();
        IModbusMaster master;
        private object _lock1 = new object();


        public void Close()
        {
            try
            {
                master.Dispose();
                serialPort.Close();
                serialPort.Dispose();
            }
            catch { }
        }

        public bool Connect(string Parameter1, int Parameter2, string Parameter3, int Parameter4, int Parameter5)
        {
            Connected = false;
            try
            {
                var DataBits = 8;
                var Parity = System.IO.Ports.Parity.None;
                var StopBits = System.IO.Ports.StopBits.One;
                if (this.serialPort.IsOpen)
                {
                    return false;
                }
                else
                {
                    switch (Parameter3)
                    {
                        case "无校验，8bit数据位，2个停止位":
                            DataBits = 8;
                            Parity = System.IO.Ports.Parity.None;
                            StopBits = System.IO.Ports.StopBits.Two;
                            break;
                        case "偶校验，8bit数据位，1个停止位":
                            DataBits = 8;
                            Parity = System.IO.Ports.Parity.Even;
                            StopBits = System.IO.Ports.StopBits.One;
                            break;
                        case "奇校验，8bit数据位，1个停止位":
                            DataBits = 8;
                            Parity = System.IO.Ports.Parity.Odd;
                            StopBits = System.IO.Ports.StopBits.One;
                            break;
                        case "无校验，8bit数据位，1个停止位":
                            DataBits = 8;
                            Parity = System.IO.Ports.Parity.None;
                            StopBits = System.IO.Ports.StopBits.One;
                            break;
                        default:
                            DataBits = 8;
                            Parity = System.IO.Ports.Parity.None;
                            StopBits = System.IO.Ports.StopBits.One;
                            break;
                    }

                    serialPort.PortName = Parameter1;
                    serialPort.BaudRate = Parameter2;
                    serialPort.DataBits = DataBits;
                    serialPort.Parity = Parity;
                    serialPort.StopBits = StopBits;
                    serialPort.Open();
                    serialPort.DiscardInBuffer();
                    serialPort.DiscardOutBuffer();
                    serialPort.WriteTimeout = Parameter4;
                    serialPort.ReadTimeout = Parameter5;
                    var factory = new ModbusFactory();
                    master = factory.CreateRtuMaster(serialPort);
                    Connected = true;
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return false;
        }

        public int ReadInovance(byte station)
        {
            object @lock = this._lock1;
            lock (@lock)
            {
                ushort[] r = master.ReadHoldingRegisters(station, 0x1507, 2);
                return Convert.ToInt32(r[1].ToString("X4") + r[0].ToString("X4"), 16);
            }
        }
    }
}
